﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Drawing;
using HCFramework.API.LINQ;

namespace HCFramework.Master.Normal.Menu
{
    public partial class Menu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadProductMenu();
                RadMenu1.Items.Where(ik => ik.Value == "Admin").First().Visible = C.UserController.IsAdmin();
                Translate();
            }
            SetActiveMenu();
        }

        private void LoadProductMenu()
        {
            List<ProductCategory> listMenuGroup = C.ProductCategoryController.ListCategoryCache.Where(ik => ik.IsActive && ik.ParentID == null).OrderBy(ik => ik.OrderDisplay).ToList();
            RadMenuItem productMenu = RadMenu1.GetAllItems().Where(ik => ik.Value == "Product").First();
            foreach (ProductCategory group in listMenuGroup)
            {
                RadMenuItem groupMenu = new RadMenuItem();
                groupMenu.Text = group.Name;
                groupMenu.NavigateUrl = "~/Default.aspx?categoryID=" + group.ProductCategoryID;
                productMenu.Items.Add(groupMenu);
                foreach (ProductCategory item in group.ProductCategories.Where(ik => ik.IsActive).OrderBy(ik => ik.OrderDisplay).ToList())
                {
                    RadMenuItem itemMenu = new RadMenuItem();
                    itemMenu.Text = item.Name;
                    itemMenu.NavigateUrl = "~/Product/Default.aspx?categoryID=" + item.ProductCategoryID;
                    groupMenu.Items.Add(itemMenu);
                }
            }
        }

        private void Translate()
        {
            foreach (RadMenuItem item in RadMenu1.GetAllItems())
            {
                item.Text = M.Common.GetLang(item.Text);
            }
        }

        private void SetActiveMenu()
        {
            foreach (RadMenuItem item in RadMenu1.GetAllItems())
            {
                if (item.NavigateUrl.ToLower() == C.TrangController.GetCurrentPage().ToLower())
                {
                    //item.BackColor = Color.FromName("#999966");
                    item.HighlightPath();
                }
            }
        }
    }
}